package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysCustomer;
import com.ruoyi.system.mapper.SysCustomerMapper;
import com.ruoyi.system.service.ISysCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 客户信息表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-03
 */
@Service
public class SysCustomerServiceImpl implements ISysCustomerService {

    @Autowired
    private SysCustomerMapper sysCustomerMapper;

    /**
     * 查询客户信息表
     *
     * @param id 客户信息表主键
     * @return 客户信息表
     */
    @Override
    public SysCustomer selectSysCustomerById(Long id) {
        return sysCustomerMapper.selectSysCustomerById(id);
    }

    /**
     * 查询客户信息表列表
     *
     * @param sysCustomer 客户信息表
     * @return 客户信息表
     */
    @Override
    public List<SysCustomer> selectSysCustomerList(SysCustomer sysCustomer) {
        return sysCustomerMapper.selectSysCustomerList(sysCustomer);
    }

    /**
     * 新增客户信息表
     *
     * @param sysCustomer 客户信息表
     * @return 结果
     */
    @Override
    public int insertSysCustomer(SysCustomer sysCustomer) {
        return sysCustomerMapper.insertSysCustomer(sysCustomer);
    }

    /**
     * 修改客户信息表
     *
     * @param sysCustomer 客户信息表
     * @return 结果
     */
    @Override
    public int updateSysCustomer(SysCustomer sysCustomer) {
        return sysCustomerMapper.updateSysCustomer(sysCustomer);
    }

    /**
     * 批量删除客户信息表
     *
     * @param ids 需要删除的客户信息表主键
     * @return 结果
     */
    @Override
    public int deleteSysCustomerByIds(Long[] ids) {
        return sysCustomerMapper.deleteSysCustomerByIds(ids);
    }

    /**
     * 删除客户信息表信息
     *
     * @param id 客户信息表主键
     * @return 结果
     */
    @Override
    public int deleteSysCustomerById(Long id) {
        return sysCustomerMapper.deleteSysCustomerById(id);
    }

    /**
     * 导入用户数据
     *
     * @param customerList 用户数据列表
     * @return 结果
     */
    @Override
    public String importCustomer(List<SysCustomer> customerList, boolean updateSupport) {
        if (StringUtils.isNull(customerList) || customerList.size() == 0) {
            throw new ServiceException("导入客户数据不能为空！" );
        }
        LoginUser loginUser = getLoginUser();
        if (updateSupport) {
            List<SysCustomer> update = new ArrayList<>();
            List<SysCustomer> insert = new ArrayList<>();
            for (SysCustomer customer : customerList) {
                SysCustomer customer1 = sysCustomerMapper.selectSysCustomerByLicense(customer.getLicense());
                if (customer1 != null) {
                    update.add(customer);
                }else {
                    insert.add(customer);
                }
            }
            if (insert.size() > 0) {
                insert.forEach(value->sysCustomerMapper.insertSysCustomer(value));
            }
            if (update.size() > 0) {
                update.forEach(value->sysCustomerMapper.updateSysCustomer(value));
            }
        }else {
            for (SysCustomer customer : customerList) {
                customer.setUserId(loginUser.getUserId());
                customer.setDeptId(loginUser.getDeptId());
                sysCustomerMapper.insertSysCustomer(customer);
            }
        }
        return "导入成功";
    }

    @Override
    public SysCustomer selectSysCustomerByLicense(String license) {
        return sysCustomerMapper.selectSysCustomerByLicense(license);
    }
}
